﻿#include "SplashScreen.h"

SplashScreen::SplashScreen()
    : progress_(0.0f)
    , m_pImage(nullptr)
    , m_progressRect(0, 0, 0, 0)
    , m_progressColor(255, 0, 120, 215) // 默认蓝色进度条
{
  // 初始化GDI+
  InitializeGdiPlus();
}

SplashScreen::~SplashScreen() {
  Release();
  ShutdownGdiPlus();
}

void SplashScreen::DrawSplashScreen(HWND hWnd) {
  // 获取窗口尺寸
  RECT rc;
  GetClientRect(hWnd, &rc);

  // 开始绘制
  PAINTSTRUCT ps;
  HDC hdc = BeginPaint(hWnd, &ps);
  {
    // 创建内存DC实现双缓冲
    HDC memDC = CreateCompatibleDC(hdc);
    HBITMAP hBmp = CreateCompatibleBitmap(hdc, rc.right, rc.bottom);
    SelectObject(memDC, hBmp);

    // 使用GDI+绘制
    Gdiplus::Graphics graphics(memDC);
    graphics.DrawImage(m_pImage, 0, 0, rc.right, rc.bottom);

    // 拷贝到窗口DC
    BitBlt(hdc, 0, 0, rc.right, rc.bottom, memDC, 0, 0, SRCCOPY);

    // 清理资源
    DeleteObject(hBmp);
    DeleteDC(memDC);
  }
  EndPaint(hWnd, &ps);
}

void SplashScreen::DoPaint(HWND hWnd) {
  if (!hWnd || !m_pImage) return;

  PAINTSTRUCT ps;
  HDC hdc = BeginPaint(hWnd, &ps);

  // 创建内存DC用于双缓冲
  HDC hdcMem = CreateCompatibleDC(hdc);
  HBITMAP hbmMem =
      CreateCompatibleBitmap(hdc, m_pImage->GetWidth(), m_pImage->GetHeight());
  HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);

  // 使用GDI+绘制
  Gdiplus::Graphics graphics(hdcMem);

  // 绘制背景图片
  graphics.DrawImage(
      m_pImage, 0, 0, m_pImage->GetWidth(), m_pImage->GetHeight());

  // 绘制进度条背景
  Gdiplus::SolidBrush bgBrush(Gdiplus::Color(100, 50, 50, 50));
  graphics.FillRectangle(&bgBrush, m_progressRect);

  // 绘制进度条前景
  Gdiplus::Rect progressFillRect(m_progressRect.X, m_progressRect.Y,
      static_cast<INT>(m_progressRect.Width * progress_), m_progressRect.Height);

  Gdiplus::SolidBrush progressBrush(m_progressColor);
  graphics.FillRectangle(&progressBrush, progressFillRect);

  // 绘制到屏幕
  BitBlt(hdc, 0, 0, m_pImage->GetWidth(), m_pImage->GetHeight(), hdcMem, 0, 0,
      SRCCOPY);

  // 清理
  SelectObject(hdcMem, hbmOld);
  DeleteObject(hbmMem);
  DeleteDC(hdcMem);

  EndPaint(hWnd, &ps);
}

void SplashScreen::DrawProgressBar() {

}

void SplashScreen::InitializeGdiPlus() {
  Gdiplus::GdiplusStartupInput gdiplusStartupInput;
  Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
}

void SplashScreen::ShutdownGdiPlus() {
  if (m_gdiplusToken != 0) {
    Gdiplus::GdiplusShutdown(m_gdiplusToken);
    m_gdiplusToken = 0;
  }
}

void SplashScreen::Release() {
  if (m_pImage) {
    delete m_pImage;
    m_pImage = nullptr;
  }
}

void SplashScreen::LoadPicture(HWND hWnd, const wchar_t* imagePath) {

  // 加载PNG图片
  if (m_pImage == nullptr) {
    m_pImage = new Gdiplus::Bitmap(imagePath);
    if (m_pImage->GetLastStatus() != Gdiplus::Ok) {
      MessageBox(hWnd, L"Failed to load splash image!", L"Error", MB_ICONERROR);
      return;
    }
  }

  // 设置进度条位置 (底部，高度为20像素)
  m_progressRect = Gdiplus::Rect(0, // X
      m_pImage->GetHeight() - 20,   // Y (从底部向上20像素)
      m_pImage->GetWidth(),         // 宽度
      20                            // 高度
  );

  progress_rect.left = m_progressRect.GetLeft();
  progress_rect.top = m_progressRect.GetTop();
  progress_rect.right = m_progressRect.GetRight();
  progress_rect.bottom = m_progressRect.GetBottom();
}
